﻿using System;
using UnityEngine;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000EA9 RID: 3753
	internal class XTouch : XSingleton<XTouch>
	{
		// Token: 0x0600C7FA RID: 51194 RVA: 0x002CC118 File Offset: 0x002CA318
		public static bool PointOnUI(Vector3 point)
		{
			Ray ray = XSingleton<XGameUI>.singleton.UICamera.ScreenPointToRay(point);
			RaycastHit raycastHit;
			return Physics.Raycast(ray, out raycastHit, float.PositiveInfinity, 32) && !raycastHit.collider.CompareTag("ChatUI");
		}

		// Token: 0x0600C7FB RID: 51195 RVA: 0x002CC168 File Offset: 0x002CA368
		public static bool IsActiveTouch(XTouchItem touch)
		{
			return touch.Phase != TouchPhase.Ended && touch.Phase != TouchPhase.Canceled;
		}

		// Token: 0x0600C7FC RID: 51196 RVA: 0x002CC192 File Offset: 0x002CA392
		public void Update()
		{
			XSingleton<XGesture>.singleton.ClearOneHit();
			this.UpdateTouch();
		}

		// Token: 0x0600C7FD RID: 51197 RVA: 0x002CC1A8 File Offset: 0x002CA3A8
		private void UpdateTouch()
		{
			int num = 0;
			while (num < Input.touchCount && num < XTouch.MultiTouchCount)
			{
				this._touches.Fake = false;
				this._touches.touch = Input.GetTouch(num);
				this.HandleTouch(this._touches);
				num++;
			}
			if (XSingleton<XKeyboard>.singleton.Enabled)
			{
				XSingleton<XKeyboard>.singleton.Update();
				for (int i = 0; i < XSingleton<XKeyboard>.singleton.touchCount; i++)
				{
					this.HandleTouch(XSingleton<XKeyboard>.singleton.GetTouch(i));
				}
			}
			//zcg 
// 			if (XSingleton<XSirJoystick>.singleton.Enabled)
// 			{
// 				XSingleton<XSirJoystick>.singleton.Update();
// 				for (int j = 0; j < XSingleton<XSirJoystick>.singleton.touchCount; j++)
// 				{
// 					this.HandleTouch(XSingleton<XSirJoystick>.singleton.GetTouch(j));
// 				}
// 			}

			if (XSingleton<XGyroscope>.singleton.Enabled)
			{
				XSingleton<XGyroscope>.singleton.Update();
				for (int k = 0; k < XSingleton<XGyroscope>.singleton.touchCount; k++)
				{
					this.HandleTouch(XSingleton<XGyroscope>.singleton.GetTouch(k));
				}
			}
			this.UpdateScreenSave();
		}

		// Token: 0x0600C7FE RID: 51198 RVA: 0x002CC2F8 File Offset: 0x002CA4F8
		private void UpdateScreenSave()
		{
			if (this._on_screen_save)
			{
				if (Input.touchCount > 0)
				{
					XSingleton<XUpdater.XUpdater>.singleton.XPlatform.ResetScreenLightness();
					this._on_screen_save = false;
					this._screenSave = 0f;
				}
			}
			else
			{
				if (Input.touchCount == 0)
				{
					this._screenSave += Time.unscaledDeltaTime;
				}
				else
				{
					this._screenSave = 0f;
				}
			}

			if (this._screenSave > XSingleton<XGlobalConfig>.singleton.ScreenSaveLimit)
			{
				this._on_screen_save = true;
				this._screenSave = 0f;
				XSingleton<XUpdater.XUpdater>.singleton.XPlatform.SetScreenLightness(XSingleton<XGlobalConfig>.singleton.ScreenSavePercentage);
			}
		}

		// Token: 0x0600C7FF RID: 51199 RVA: 0x002CC3BC File Offset: 0x002CA5BC
		private void HandleTouch(XTouchItem touch)
		{
			if (XTouch.PointOnUI(touch.Position))
			{
				switch (touch.Phase)
				{
				case 0:
				{
					if (touch.Fake)
					{
						touch.faketouch.phase = TouchPhase.Canceled;
					}
					else
					{
						touch.Convert2FakeTouch(TouchPhase.Canceled);
					}
					break;
				}
				}
			}
			XSingleton<XVirtualTab>.singleton.Feed(touch);
			XSingleton<XGesture>.singleton.Feed(touch);
		}

		// Token: 0x0400584A RID: 22602
		public static int MultiTouchCount = 2;

		// Token: 0x0400584B RID: 22603
		private bool _on_screen_save = false;

		// Token: 0x0400584C RID: 22604
		private float _screenSave = 0f;

		// Token: 0x0400584D RID: 22605
		private XTouchItem _touches = new XTouchItem();
	}
}
